package diaguml;
//package com.mxgraph.examples.swing;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2707712944901661771L;

	public HelloWorld() throws ParserConfigurationException, SAXException, IOException
	{
		super("Hello, World!");

		mxGraph graph = new mxGraph();
		Object parent = graph.getDefaultParent();

		graph.getModel().beginUpdate();
		try
		{
			/*Object v1 = graph.insertVertex(parent, null, "Hello", 30, 20, 80,
					30);
			Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
					80, 30);
			Object v3 = graph.insertVertex(parent, null, "Salut!", 140, 50,
					80, 30);
			Object v4 = graph.insertVertex(parent, null, "Les copains!", 340, 250,
					80, 30);
			graph.insertEdge(parent, null, "Edge \n *", v1, v2);*/
			
			 JFileChooser fChooser = new JFileChooser("./xml");
				FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers XML", "xml");
				fChooser.setFileFilter(filter);
				fChooser.showOpenDialog(null);
				System.out.println(fChooser.getSelectedFile().getPath());
				
				ParseurXML parseur = new ParseurXML(fChooser.getSelectedFile().getPath());
				List<Etiquette> res = new ArrayList<Etiquette>();
				res = parseur.getEtiquettes();
				
				List<Liaison> res2 = new ArrayList<Liaison>();
				res2 = parseur.getLiaisons();
				List<Object> list_obj = new ArrayList<Object>();
				int k = 0;
				for(int i=0; i<res2.size(); i++){
				//	if ( list_obj.contains())
					Object v1 = graph.insertVertex(parent, null, res2.get(i).getEtDep().getNom(), 10*i+k, 20*i+k+20, 80,
							30);
					list_obj.add(v1);
					Object v2 = graph.insertVertex(parent, null, res2.get(i).getEtArr().getNom(), 10*i+k+100, 20*i+k+20+100, 80,
							30);
					list_obj.add(v2);
					graph.insertEdge(parent, null, "Edge", v1, v2);
					k = k + 10;
				}
				
				/*for(int i=0; i<res.size();i++){
					String nom_et1 = res2.get(i).getEtDep().getNom();
					String nom_et2 = res2.get(i).getEtArr().getNom();
					for(int j=0; j<res2.size();j++){
						if (res2.get(j).getEtDep().getNom().equals(res.get(i).getNom())){
							res2.get(j).getEtArr();
						}
						
					}
				}*/
				
				
				
		}
		finally
		{
			graph.getModel().endUpdate();
		}

		mxGraphComponent graphComponent = new mxGraphComponent(graph);
		getContentPane().add(graphComponent);
		
		Object[] tab2 =  graph.getChildCells(graph.getDefaultParent(),true,true);
		System.out.println(tab2.length+"taille du tab2");
		
		for (Object vertex: tab2){
			//graph.labelChanged(vertex, "");
			//String si = ((AbstractButton) vertex).getLabel();
			//graph.cellLabelChanged(vertex, "",false);
			mxCell vert = (mxCell) vertex;
			String s = graph.getLabel(vert.getSource());
			System.out.println("C'EST MOI LOOOl"+s);
			
		}
	}

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
	{
		HelloWorld frame = new HelloWorld();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 400);
		frame.setVisible(true);
	}

}
